package com.xtourms.client.sys;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.xtourms.client.Context;
import com.xtourms.client.sys.um.MineManager;
import com.xtourms.client.sys.um.WorkerManagerFrame;
import com.xtourms.shared.field.WorkerField;

public class SystemManagerFrame extends Composite {

	private static SystemManagerFrameUiBinder uiBinder = GWT
			.create(SystemManagerFrameUiBinder.class);

	interface SystemManagerFrameUiBinder extends
			UiBinder<Widget, SystemManagerFrame> {
	}

	private Widget cur_res;
	private Anchor cur_nav;
	@UiField HTMLPanel smf;
	@UiField HTMLPanel nothing;
	@UiField Anchor workerManager;
	@UiField Anchor mine;
	
	public SystemManagerFrame() {
		initWidget(uiBinder.createAndBindUi(this));
		cur_res = nothing;
		initFrame();
	}

	private void initFrame(){
		int droit = (int)((JSONNumber)Context.ins.worker.get(WorkerField.WORKER_DROIT)).doubleValue();
		switch(droit){
		case WorkerField.SYS_DROIT:
		case WorkerField.MANAGER_DROIT:
			switchSys(workerManager, new WorkerManagerFrame());
			break;
		case WorkerField.OPERATOR_DROIT:
			//FIXME:关闭其他管理权限
			workerManager.setVisible(false);
			switchSys(mine, new MineManager());
			break;
		default:
		}
	}
	
	private void switchSys(Anchor nextNav, Widget nextWidget) {
		if(cur_nav != nextNav){
			if(null != cur_nav){
				cur_nav.removeStyleName("cur_sub_nav");
			}
			cur_nav = nextNav;
			cur_nav.addStyleName("cur_sub_nav");
			cur_res.removeFromParent();
			cur_res = nextWidget;
			smf.add(cur_res, "sys_content");
		}
	}
	
	@UiHandler("workerManager")
	void manageWorker(ClickEvent evt){
		switchSys(workerManager, new WorkerManagerFrame());
	}
	
	@UiHandler("mine")
	void manageMine(ClickEvent evt){
		switchSys(mine, new MineManager());
	}
}
